import java.util.HashMap;
import java.util.Map;

//连续数组
public class FindMaxLength {
    public static int code(int[] nums) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0, -1);

        int sum = 0, ret = 0;
        for (int i = 0; i < nums.length; i++){
            sum += (nums[i] == 0 ? -1 : 1);
            if (hash.containsKey(sum)){
                ret = Math.max(ret, i - hash.get(sum));
            }else{
                hash.put(sum, i);
            }

        }

        return ret;
    }

    public static void main(String[] args) {
        FindMaxLength.code(new int[]{0, 1, 0});
    }
}
